package admin

import (
	"easyblog/logicals"
	"easyblog/models"
	"easyblog/utils/errmsg"
	"easyblog/utils/jwt"
	"encoding/json"
	"github.com/beego/beego/v2/server/web"
)

type LoginController struct {
	web.Controller
}

// @router /admin/login [post]
func (this *LoginController) AdminLogin() {
	var user models.User
	var code int
	var token string

	body := this.Ctx.Input.RequestBody
	_ = json.Unmarshal(body, &user)

	code = logicals.DefaultUser.CheckLogin(user.Username, user.Password)
	if code == errmsg.SUCCSE {
		token, code = jwt.GenerateToken(user.Username)
	}

	result := map[string]interface{}{
		"status":    code,
		"message": errmsg.GetErrmsg(code),
		"token":   token,
	}
	this.Data["json"] = result

	this.ServeJSON() //返回json格式
}